﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Pes.Messaging.DatabaseLayer
{
    public static class Emails
    {
        public static bool FlagSent(string id)
        {
            string sql = "UPDATE dbo.OutgoingEmails SET Sent=1 WHERE OutgoingEmailId=@OutgoingEmailId;";
            using (SqlCommand command = new SqlCommand(sql))
            {
                command.Parameters.AddWithValue("@OutgoingEmailId", id);
                return Pes.DBFactory.DBOperations.ExecuteNonQuery(command);
            }
        }

        public static bool SaveToQueue(string emailAddress, string subject, string message)
        {
            string sql = "dbo.SysMail";
            using (SqlCommand command = new SqlCommand(sql))
            { 
                command.CommandType  = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@EmailAddress", emailAddress);
                command.Parameters.AddWithValue("@Subject", subject);
                command.Parameters.AddWithValue("@Message", message);

                return Pes.DBFactory.DBOperations.ExecuteNonQuery(command);
            }
        }

        public static DataTable GetEmailQueue()
        {
            string sql = "SELECT * FROM dbo.OutgoingEmailView WHERE Sent = 0;";
            using (SqlCommand command = new SqlCommand(sql))
            {
                return Pes.DBFactory.DBOperations.GetDataTable(command);
            }
        }
    }
}
